/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package photoshare.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import photoshare.dto.CategoryDTO;
import photoshare.util.DBUtil;

/**
 *
 * @author PhotoShare
 */
public class CategoryDAO {

    private String READ_BY_ALL = "SELECT * FROM CATEGORIES";
    private String READ_BY_ID = "SELECT * FROM CATEGORIES WHERE CategoryID = ?";
    private String CREATE = "INSERT INTO CATEGORIES VALUES(?,?,?)";
    private String DELETE_BY_ID = "UPDATE CATEGORIES SET IsACTIVE= 0 WHERE CategoryID = ?";
    private String UPDATE_BY_ID = "UPDATE CATEGORIES SET PASS = ? WHERE CategoryID = ?";

    private Connection conn = null;
    private PreparedStatement pst = null;

    public CategoryDAO() {
    }

     public boolean create(CategoryDTO categoryDTO) throws SQLException {
        try {
            conn = DBUtil.getConn();
            pst = conn.prepareStatement(CREATE);
            pst.setString(1, categoryDTO.getCategoryName());
            pst.setString(2, categoryDTO.getDescription());
            int num = 0;
            num = pst.executeUpdate();
            if (num > 0) {
                return true;
            }
            return false;
        } catch (Exception ex) {
            return false;
        } finally {
            DBUtil.closeConn(conn);
            pst.close();
        }
    }

    public ArrayList<CategoryDTO> readAll() {
        try {
            ArrayList<CategoryDTO> arr = null;
            arr = new ArrayList<CategoryDTO>();
            conn = DBUtil.getConn();
            pst = conn.prepareStatement(READ_BY_ALL);
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                CategoryDTO dto = new CategoryDTO();
                dto.setCategoryName(rs.getString(2));
                dto.setDescription(rs.getString(3));
                arr.add(dto);
            }
            return arr;
        } catch (SQLException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } finally {
            DBUtil.closeConn(conn);
        }
    }

    public CategoryDTO readById(Integer categoryId) {
        try {
            conn = DBUtil.getConn();
            pst = conn.prepareStatement(READ_BY_ID);
            pst.setInt(1, categoryId);
            //pst = conn.prepareStatement("select * from acc WHERE accountID = 'admin'");
            ResultSet rs = pst.executeQuery();
            CategoryDTO albumDTO = new CategoryDTO();
            while (rs.next()) {
                albumDTO.setCategoryName(rs.getString(2));
                albumDTO.setDescription(rs.getString(3));

            }
            return albumDTO;
        } catch (SQLException ex) {
            Logger.getLogger(AccountDAO.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } finally {
            DBUtil.closeConn(conn);
        }
    }

    public boolean updateById(CategoryDTO categoryDTO, Integer categoryId) throws SQLException {
        try {
            conn = DBUtil.getConn();
            pst = conn.prepareStatement(UPDATE_BY_ID);
            //Tham so can Update trong cau lenh SQL
            pst.setString(1, categoryDTO.getCategoryName());
            //Tham so dieu kien cua cau lenh Update
            pst.setInt(2, categoryId);
            int num = 0;
            num = pst.executeUpdate();
            if (num > 0) {
                return true;
            }
            return false;
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            return false;
        } finally {
            DBUtil.closeConn(conn);
            pst.close();
        }
    }

    public boolean deleteById(Integer categoryId) throws SQLException {

        try {
            conn = DBUtil.getConn();
            pst = conn.prepareStatement(DELETE_BY_ID);
            //Tham so dieu kien cho cau lenh Update
            pst.setInt(1, categoryId);
            int num = 0;
            num = pst.executeUpdate();
            if (num > 0) {
                return true;
            }
            return false;
        } catch (Exception ex) {
            return false;
        } finally {
            DBUtil.closeConn(conn);
            pst.close();
        }
    }
        
}
